import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录路由组件
import Login from '../components/Login.vue'
// 导入Home组件
import Home from '../components/Home.vue'
//导入home 子组件welcome
import Welcome from '../components/Welcome.vue'
//导入用户 子组件users
import Users from '../components/user/users.vue'
//导入权限 子组件Rights
import Rights from '../components/power/Rights.vue'
//导入权限 子组件Roles
import Roles from '../components/power/Roles.vue'
//导入商品分类 子组件Cate
import Cate from '../components/goods/Cate.vue'
//导入商品参数 子组件Params
import Params from '../components/goods/Params.vue'
//导入商品列表 子组件List
import List from '../components/goods/List.vue'
//导入添加商品 子组件Add
import Add from '../components/goods/Add.vue'
//导入订单组件 Order
import Order from '../components/order/Order.vue'
//导入数据报表组件 Report
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    //路由重定向到Login
    { path: '/', component: Login },
    //登录路由规则
    { path: '/login', component: Login },
    //主页Home路由规则
    {
      path: '/home', component: Home, redirect: '/welcome', children: [
        // home子路由welcome规则
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report },
      ]
    },
  ]
})

router.beforeEach((to, from, next) => {
  //访问登录页面  放行
  if (to.path === '/login') return next();
  //如果没有token 强制跳转到登录页
  const tokenStr = window.sessionStorage.getItem('token')
  if (tokenStr) return next();
  if (!tokenStr) return next('/login')
})

export default router
